#!/usr/bin/env python3

import os
import sys
import asyncio
from websockets.sync.client import connect
import threading
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--host', type=str, default='localhost', help='default: localhost')
parser.add_argument('--port', type=int, default=9527, help='default: 9527')
args = parser.parse_args()

ws = connect(f'ws://{args.host}:{args.port}')

def recv():
    try:
        while True:
            message = ws.recv()
            print(message, end='', flush=True)
    except:
        os._exit(0)

print('>> type `.quit` or `CTRL-C CTRL-C` to exit')

threading.Thread(target=recv, daemon=True).start()

try:
    while True:
        cmd = input('')
        if cmd == '.quit': os._exit(0)
        ws.send(cmd)
except:
    os._exit(0)

